﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;

namespace RemoteImaging.LicensePlate
{
    public class SuspectCarFormPresenter : INotifySuspeciousLicensePlate
    {
        private readonly IMessageBoxService _messageBoxService;
        private SuspeciousCarAlermInfo _currentAlerm;
        private SpeakService _speaker;
        private SoundPlayer _soundPlayer;
        bool _showSoundError = true;

        public SuspectCarFormPresenter(IMessageBoxService messageBoxService,
                                       ILicensePlateEventPublisher licensePlateEventPublisher)
        {
            if (messageBoxService == null) throw new ArgumentNullException("messageBoxService");

            _messageBoxService = messageBoxService;
            _soundPlayer = new SoundPlayer { SoundLocation = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Settings.Default.alermWaveFile) };
        }

        public void Notify(SuspeciousCarAlermInfo alerm)
        {
            _currentAlerm = alerm;

            SoundVoiceAlerm(_currentAlerm);

            Func<System.Windows.Forms.Form> doit = () =>
                                                       {
                                                           var form = new FormSuspeciousCar();
                                                           form.DataContext = _currentAlerm;

                                                           form.AttachPresenter(this);
                                                           return form;
                                                       };
            _messageBoxService.ShowForm(doit);

        }

        private void SoundVoiceAlerm(SuspeciousCarAlermInfo alerm)
        {
            try
            {
                if (Properties.Settings.Default.useWaveFileToAlert)
                {
                    _soundPlayer.PlayLooping();
                }
                else
                {
                    var textToSpeak = string.Format(Properties.Settings.Default.VoiceAlertTemplate,
                                                    alerm.CapturedLicenseInfo.LicensePlateNumber);
                    _speaker = SpeakService.Speak(textToSpeak);
                }
            }
            catch (Exception e)
            {
                if (_showSoundError)
                {
                    _messageBoxService.ShowError("播放声音报警出现错误，请确认声音报警设置正确无误。" + "\r\n本信息将不再提示！");
                }
                _showSoundError = false;
            }
        }

        public void Mute()
        {
            if (_speaker != null)
            {
                _speaker.MuteAll();
            }

            _soundPlayer.Stop();
        }

        public void ConfirmAlarm()
        {
            ProcessAlerm(ProcessBehavior.Confirmed);
        }

        private void ProcessAlerm(ProcessBehavior processBehavior)
        {
            if (_currentAlerm != null)
            {
                _currentAlerm.ProcessBehavior = processBehavior;
                _currentAlerm.HandleTime = DateTime.Now;

            }
        }

        private SuspectCarAlermHandleInfo CreateSuspectCarHandleInfo()
        {
            return new SuspectCarAlermHandleInfo(_currentAlerm);
        }

        public void DiscardAlerm()
        {
            ProcessAlerm(ProcessBehavior.Ignored);}
    }
}
